//
//  ViewController.m
//  08-homeWork-通讯录
//
//  Created by 402030900@qq.com on 16/7/25.
//  Copyright © 2016年 402030900@qq.com. All rights reserved.
//

#import "ViewController.h"
#import "DetailViewController.h"
#import "AddViewController.h"

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource,passValue>
@property(nonatomic,strong)UITableView * tableView;
@property(nonatomic,strong)NSMutableArray * stuArray;
@property(nonatomic,strong)NSMutableArray * nameArray;
@property(nonatomic,strong)NSIndexPath * index;
@property(nonatomic,strong)NSMutableDictionary * studentDic;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.hidden = NO;
    self.navigationItem.title = @"通讯录";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:(UIBarButtonSystemItemAdd) target:self action:@selector(addBarButton:)];
    NSString * path = [[NSBundle mainBundle]pathForResource:@"DLS160710(1)" ofType:@"plist"];
     _stuArray = [NSMutableArray arrayWithContentsOfFile:path];
//    NSLog(@"%@",_stuArray);
 
    _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:(UITableViewStylePlain)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:_tableView];
    
    
    
    // Do any additional setup after loading the view, typically from a nib.
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSLog(@"count = %ld",_stuArray.count);
    return _stuArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    _nameArray = [NSMutableArray array];
    for (NSDictionary * dicTemp in _stuArray) {
       [_nameArray addObject:[dicTemp objectForKey:@"name"]];
        NSLog(@"%@",_nameArray);
    }
    
    cell.textLabel.text = _nameArray[indexPath.row] ;
//    cell.textLabel.text = [_stuArray[indexPath.row] objectForKey:@"name"];
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    DetailViewController * detailVC= [[DetailViewController alloc] init];
    detailVC.name = [_stuArray[indexPath.row]objectForKey:@"name"];
    detailVC.age = [_stuArray[indexPath.row]objectForKey:@"age"];
    detailVC.gender = [_stuArray[indexPath.row]objectForKey:@"gender"];
    detailVC.tele = [_stuArray[indexPath.row]objectForKey:@"phoneNumber"];
//    self.index = indexPath;
    [self.navigationController pushViewController:detailVC animated:YES];
}
-(void)addBarButton:(UIButton *)button {
    AddViewController * addVC= [[AddViewController alloc]init];
    addVC.delegate = self;
    [self.navigationController pushViewController:addVC animated:YES];
}

-(void)PassValue:(NSDictionary *)dic {
   
    [_stuArray addObject:dic];
    [_tableView reloadData];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
